﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNet.Identity;
using QuickPrompt.DomainModel;
using QuickPromt.Service;

namespace QuickPrompt.Spa.Controllers
{
    [Authorize]
    public class NoteController : BaseController
    {
        private readonly IQuickNoteService _quickNoteService;

        public NoteController(IQuickNoteService quickNoteService)
        {
            _quickNoteService = quickNoteService;
        }

        // GET: Note
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Read([DataSourceRequest] DataSourceRequest request)
        {
            IEnumerable<QuickNote> notes = _quickNoteService.Get(User.Identity.GetUserId());

            var filteredProperties = notes.Select(
                x => new {x.Id, x.Name, x.UserGuid, x.Description, x.IsActive, x.DateCreatedUtc, x.StartDateUtc, x.EndDateUtc})
                .ToList();
            return Json(filteredProperties.ToDataSourceResult(request));
        }

        [HttpPost]
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, QuickNote note)
        {
            _quickNoteService.Insert(note);
            _quickNoteService.SaveChangesAsync();
            return Json(true);
        }

        [HttpPost]
        public ActionResult Update([DataSourceRequest] DataSourceRequest request, QuickNote note)
        {
            _quickNoteService.Update(note);
            _quickNoteService.SaveChangesAsync();
            return Json(true);
        }

        [HttpPost]
        public ActionResult Destroy([DataSourceRequest] DataSourceRequest request, int id)
        {
            _quickNoteService.SetToDelete(id);
            _quickNoteService.SaveChangesAsync();
            return Json(true);
        }
    }
}